/*
 * ============================================================================
 *
 *  [THC RPG] Total HardCore RPG
 *
 *  File:          rpgmenu.inc
 *  Type:          Core module
 *  Description:   User menu for rpg functions
 *  Author:        ArsiRC
 *
 *  Copyright (C) 2009-2011  ArsiRC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ============================================================================
 */

/**
 * This module's identifier.
 */
new Module:g_moduleRPGMenu;

/**
 * Function for outside files to use to return the module's identifier.
 */
stock Module:RPGMenu_GetIdentifier() { return g_moduleRPGMenu; }

/**
 * Called when RPGMenu is created.
 *
 * @param hMenu     Handle to RPGMenu.
 */
new ProjectEvent:g_evOnRPGMenuCreated;

/**
 * Called when RPGMenu is deleted.
 *
 * @param hMenu     Handle to RPGMenu.
 */
new ProjectEvent:g_evOnRPGMenuDeleted;

/**
 * Handle to RPGMenu.
 */
new Handle:g_hRPGMenu;
new Handle:g_hRPGMenuUpgrades;
new Handle:g_hRPGMenuStats;
new Handle:g_hRPGMenuInfo;

/**
 * Register this module.
 */
RPGMenu_Register()
{
    // Define all the module's data as layed out by enum ModuleData in project.inc.
    new moduledata[ModuleData];

    moduledata[ModuleData_Disabled] = false;
    moduledata[ModuleData_Hidden] = false;
    strcopy(moduledata[ModuleData_FullName], MM_DATA_FULLNAME, "RPGMenu");
    strcopy(moduledata[ModuleData_ShortName], MM_DATA_SHORTNAME, "rpgmenu");
    strcopy(moduledata[ModuleData_Description], MM_DATA_DESCRIPTION, "User menu for rpg functions");
    moduledata[ModuleData_Dependencies][0] = INVALID_MODULE;

    // Send this array of data to the module manager.
    g_moduleRPGMenu = ModuleMgr_Register(moduledata);

    // Register the OnEventsRegister event to register all events in it.
    EventMgr_RegisterEvent(g_moduleRPGMenu, "Event_OnEventsRegister",             "RPGMenu_OnEventsRegister");

    // Create custom events.
    g_evOnRPGMenuCreated = EventMgr_CreateEvent("Event_OnRPGMenuCreated");
    g_evOnRPGMenuDeleted = EventMgr_CreateEvent("Event_OnRPGMenuDeleted");
}

/**
 * Register all events here.
 */
public RPGMenu_OnEventsRegister()
{
    // Register all the events needed for this module.
    EventMgr_RegisterEvent(g_moduleRPGMenu, "Event_OnAllModulesLoaded",           "RPGMenu_OnAllModulesLoaded");
    EventMgr_RegisterEvent(g_moduleRPGMenu, "Event_OnMyModuleEnable",             "RPGMenu_OnMyModuleEnable");
    EventMgr_RegisterEvent(g_moduleRPGMenu, "Event_OnMyModuleDisable",            "RPGMenu_OnMyModuleDisable");
}

/**
 * Plugin is loading.
 */
RPGMenu_OnPluginStart()
{
    // Register the module.
    RPGMenu_Register();

    // Register the RPGMenu command.
    RegConsoleCmd("rpgmenu", Command_RPGMenu);
}

/**
 * All modules have been registered at this point.
 */
public RPGMenu_OnAllModulesLoaded()
{
    RPGMenu_CreateMenu();
}

/**
 * The module that hooked this event callback has been enabled.
 *
 * @param refusalmsg    The string that is printed if Plugin_Handled is returned and it is non-empty.
 * @param maxlen        The max length of the string.
 *
 * @return              Return Plugin_Handled to stop enable, and Plugin_Continue to allow it.
 */
public Action:RPGMenu_OnMyModuleEnable(String:refusalmsg[], maxlen)
{
    RPGMenu_CreateMenu();
}

/**
 * The module that hooked this event callback has been disabled.
 *
 * @param refusalmsg    The string that is printed if Plugin_Handled is returned and it is non-empty.
 * @param maxlen        The max length of the string.
 *
 * @return              Return Plugin_Handled to stop disable, and Plugin_Continue to allow it.
 */
public Action:RPGMenu_OnMyModuleDisable(String:refusalmsg[], maxlen)
{
    RPGMenu_DeleteMenu();
}

RPGMenu_CreateMenu()
{
    // Register menu with menulib.
    g_hRPGMenu = MenuLib_CreateMenu("RPGMenu", INVALID_FUNCTION, INVALID_FUNCTION, "RPGMenu title", true, false, false);
    g_hRPGMenuUpgrades = MenuLib_CreateMenu("RPGMenuUpgrades", INVALID_FUNCTION, INVALID_FUNCTION, "RPGMenu upgrades title", true, false, false);
    g_hRPGMenuStats = MenuLib_CreateMenu("RPGMenuStats", INVALID_FUNCTION, INVALID_FUNCTION, "RPGMenu stats title", true, false, false);
    g_hRPGMenuInfo = MenuLib_CreateMenu("RPGMenuInfo", INVALID_FUNCTION, INVALID_FUNCTION, "RPGMenu info title", true, false, false);

    MenuLib_AddMenuBtnEx(g_hRPGMenu, "RPGMenu upgrades item", "", true, ITEMDRAW_DEFAULT, INVALID_FUNCTION, BtnNextMenu_LinkMenu, "RPGMenuUpgrades");
    MenuLib_AddMenuBtnEx(g_hRPGMenu, "RPGMenu stats item", "", true, ITEMDRAW_DEFAULT, INVALID_FUNCTION, BtnNextMenu_LinkMenu, "RPGMenuStats");
    MenuLib_AddMenuBtnEx(g_hRPGMenu, "RPGMenu info item", "", true, ITEMDRAW_DEFAULT, INVALID_FUNCTION, BtnNextMenu_LinkMenu, "RPGMenuInfo");

    // Forward event to all modules.
    new any:eventdata[1][1];
    eventdata[0][0] = g_hRPGMenu;
    EventMgr_Forward(g_evOnRPGMenuCreated, eventdata, sizeof(eventdata), sizeof(eventdata[]), g_CommonDataType2);
}

RPGMenu_DeleteMenu()
{
    // Forward event to all modules.
    new any:eventdata[1][1];
    eventdata[0][0] = g_hRPGMenu;
    EventMgr_Forward(g_evOnRPGMenuDeleted, eventdata, sizeof(eventdata), sizeof(eventdata[]), g_CommonDataType2);

    // Delete the entire menu tree.
    MenuLib_DeleteMenu(g_hRPGMenu);
}

/**
 * Command callback (RPGMenu)
 * Open up RPGMenu.
 *
 * @param client    The client index.
 * @param argc      Argument count.
 */
public Action:Command_RPGMenu(client, argc)
{
    // Check if the module is disabled.
    if (ModuleMgr_IsDisabled(g_moduleRPGMenu))
        return Plugin_Continue;

    if (client == SERVER_INDEX)
    {
        TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Reply, INVALID_MODULE, false, "Must be player");
        return Plugin_Handled;
    }

    MenuLib_DisplayMenu(g_hRPGMenu, client);
    return Plugin_Handled;
}
